using Microsoft.Extensions.FileProviders;
using System.IO;
using Microsoft.EntityFrameworkCore;
using RestfulApi.Data;
using RestfulApi.Controllers;

// 创建Web应用程序构建器实例
var builder = WebApplication.CreateBuilder(args);

// 向依赖注入容器添加控制器服务
// AddNewtonsoftJson()添加对JSON序列化的Newtonsoft.Json支持
builder.Services.AddControllers().AddNewtonsoftJson();
builder.Services.AddScoped<TestFilterController>();

// 添加API浏览器服务，用于生成OpenAPI规范
builder.Services.AddEndpointsApiExplorer();
// 添加Swagger生成器服务，用于生成Swagger文档
builder.Services.AddSwaggerGen();

// 添加目录浏览服务
builder.Services.AddDirectoryBrowser();

// 添加Entity Framework Core SQLite数据库上下文
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection") ?? "Data Source=bookstore.db"));

// 添加图书仓储服务
builder.Services.AddScoped<IBookRepository, BookRepository>();

// 构建Web应用程序实例
var app = builder.Build();

// 确保数据库已创建并应用迁移
using (var scope = app.Services.CreateScope())
{
    var services = scope.ServiceProvider;
    try
    {
        var context = services.GetRequiredService<ApplicationDbContext>();
        context.Database.EnsureCreated();
    }
    catch (Exception ex)
    {
        var logger = services.GetRequiredService<ILogger<Program>>();
        logger.LogError(ex, "初始化数据库时出错");
    }
}

// 配置HTTP请求处理管道
// 检查当前环境是否为开发环境
// 在开发环境中启用Swagger中间件
app.UseSwagger();
// 在开发环境中启用SwaggerUI中间件，提供API文档界面
app.UseSwaggerUI();

// 添加HTTPS重定向中间件，将HTTP请求重定向到HTTPS
app.UseHttpsRedirection();

// 添加静态文件中间件，启用静态文件服务
app.UseStaticFiles();

// 添加目录浏览中间件，启用文件目录浏览功能
app.UseDirectoryBrowser();

// 配置特定目录的浏览功能
app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(
        Path.Combine(builder.Environment.ContentRootPath, "wwwroot", "files")),
    RequestPath = "/files"
});

app.UseDirectoryBrowser(new DirectoryBrowserOptions
{
    FileProvider = new PhysicalFileProvider(
        Path.Combine(builder.Environment.ContentRootPath, "wwwroot", "files")),
    RequestPath = "/files"
});

app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(
        Path.Combine(builder.Environment.ContentRootPath, "wwwroot", "documents")),
    RequestPath = "/documents"
});

app.UseDirectoryBrowser(new DirectoryBrowserOptions
{
    FileProvider = new PhysicalFileProvider(
        Path.Combine(builder.Environment.ContentRootPath, "wwwroot", "documents")),
    RequestPath = "/documents"
});

app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(
        Path.Combine(builder.Environment.ContentRootPath, "wwwroot", "images")),
    RequestPath = "/images"
});

app.UseDirectoryBrowser(new DirectoryBrowserOptions
{
    FileProvider = new PhysicalFileProvider(
        Path.Combine(builder.Environment.ContentRootPath, "wwwroot", "images")),
    RequestPath = "/images"
});

app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(
        Path.Combine(builder.Environment.ContentRootPath, "wwwroot", "bookstore")),
    RequestPath = "/bookstore"
});

app.UseDirectoryBrowser(new DirectoryBrowserOptions
{
    FileProvider = new PhysicalFileProvider(
        Path.Combine(builder.Environment.ContentRootPath, "wwwroot", "bookstore")),
    RequestPath = "/bookstore"
});

// 添加授权中间件，处理授权逻辑
app.UseAuthorization();

// 添加终结点路由中间件，将请求路由到控制器操作方法
app.MapControllers();

// 启动应用程序，开始监听HTTP请求
app.Run();